//
//  HYFEmoticonTools.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/29.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

//最大行数
let HYFEmoticonMaxRow = 3
//最大列数
let HYFEmoticonMaxCol = 7
//最多表情数
let HYFEmoticonMaxCount = HYFEmoticonMaxRow * HYFEmoticonMaxCol - 1


class HYFEmoticonTools: NSObject {

    //全局访问点
    static let sharedTools:HYFEmoticonTools = HYFEmoticonTools()
    
    // emotion bundle 文件
    lazy var emotionBundle:NSBundle = {
        
       //路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        //返回 文件
        
        return NSBundle(path:path)!
    }()
    
    //默认表情
    lazy var defaultEmoticons:[HYFEmoticon] = {
        
        return self.getEmoticons("default/")
        
    }()
    
    //emoji 表情
    lazy var emojiEmoticons:[HYFEmoticon] = {
        
        return self.getEmoticons("emoji/")
    }()
    
    lazy var lxhEmoticons:[HYFEmoticon] = {
        
        return self.getEmoticons("lxh/")
    }()
    
    
    
    //懒加载 3 维数组
    lazy var allEmoticons:[[[HYFEmoticon]]] = {
        return [
            self.getEmoticonsGroup(self.defaultEmoticons),
            self.getEmoticonsGroup(self.emojiEmoticons),
            self.getEmoticonsGroup(self.lxhEmoticons)
        ]
    }()

    
    /*
     
     默认表情 108
     emoji  80
     浪小花  40
     
     通过一维数组 得到二位数组
     默认表情(6页)
     [[20],[20],[20],[20],[20],[8]]
     [0,19] [20,39] [40,59] .....
     emoji(4页)
     [[20],[20],[20],[20]]
     浪小花（2页）
     [[20],[20]]
     */
    
    
    //获得二维数组
    func getEmoticonsGroup(emoticons:[HYFEmoticon]) -> [[HYFEmoticon]]{
        
        //计算页数
        let pageCount = (emoticons.count + HYFEmoticonMaxCount - 1)/HYFEmoticonMaxCount
        //定义一个临时可变的二维数组
        var tempArray:[[HYFEmoticon]] = [[HYFEmoticon]]()
        
        for i in 0..<pageCount{
            
            //位置
            let location = HYFEmoticonMaxCount * i
            //长度
            var length = HYFEmoticonMaxCount
            //因为默认表情为 108 ,会导致越界,需要计算
            if location + length > emoticons.count{
                
                length = emoticons.count - location
            }
            //range
            let range = NSRange(location: location, length: length)
            //截取最后的一维数组
            let array = (emoticons as NSArray).subarrayWithRange(range) as! [HYFEmoticon]
            //保存数组
            tempArray.append(array)
            
        }
        return tempArray
    }
    
    
    //获得一维数组
    func getEmoticons(name:String) -> [HYFEmoticon]{
        
        //路径
        let file = emotionBundle.pathForResource("\(name)info.plist", ofType: nil)!
        //plist 文件转数组
        let array = NSArray(contentsOfFile: file)!
        //创建一个临时可变数组
        var tempArray:[HYFEmoticon] = [HYFEmoticon]()
        //遍历数组,字典转模型
        for dic in array{
            //创建一个模型
            let emoticon = HYFEmoticon(dict: dic as! [String : AnyObject])
            emoticon.path = "\(name)\(emoticon.png ?? "")"
            
            //保存模型
            tempArray.append(emoticon)
        }
        
        return tempArray
    }
    
    //传入图片名称得到相应的模型
    func searchEmoticons(chs:String?) -> HYFEmoticon?{
        
        //遍历默认表情数组
        for emoticon in defaultEmoticons{
            
            if emoticon.chs == chs {
                
                return emoticon
            }
        }
        
        //遍历浪小花表情数组
        for emoticon in lxhEmoticons{
            
            if emoticon.chs == chs {
                
                return emoticon
            }
        }
        return nil
    }
}
